<?php

/**
 * @todo refactor so we call Domain51_Model_Mapper directly with a "type"
 */
Solar::loadClass('Domain51_Model_Mapper_Sql');
Solar::loadClass('Domain51_Controller_Abstract', true);

abstract class Domain51_Controller_Model extends Domain51_Controller_Abstract
{
    protected $_modelMapper = null;
    protected $_view = null;
    
    public function __construct($config = null) {
        parent::__construct($config);
        
        $exploded = explode('_', get_class($this));
        array_pop($exploded);
        $name = ucfirst(strtolower(array_pop($exploded)));
        $this->_modelMapper = new Domain51_Model_Mapper_Sql($name);
        
        $this->_initView();
    }
    
    public function dispatch($spec) {
        // todo: account for strings
        $path = $spec->path;
        $action = 'action' . ucfirst(strtolower(array_shift($path)));
        return call_user_func_array(array($this, $action), $path);
    }
    
    protected function actionView($id) {
        $model = $this->_modelMapper->fetchById($id);
        
        $this->_view->model = $model;
        return $this->_view->fetch('view.html');
    }
}

?>